package ljt.music.ui.main;

import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.view.MenuItem;
import android.widget.Toast;

import com.ljq.mvpframework.factory.CreatePresenter;

import ljt.music.R;
import ljt.music.module.base.BaseMvpActivity;
import ljt.music.module.blank.BlankFragment;
import ljt.music.module.tab2.Tab2Fragment;
import ljt.music.module.tab3.Tab3Fragment;
import ljt.music.module.tab4.Tab4Fragment;
import ljt.music.widget.helper.BottomNavigationViewHelper;

/**
 * @author jiangtao
 */
@CreatePresenter(MainActivityPresenter.class)
public class MainActivity extends BaseMvpActivity<MainActivityView, MainActivityPresenter> implements MainActivityView, BlankFragment.OnFragmentInteractionListener {

    private static final String TAG = "MainActivity";
    private static final String SELECT_ITEM = "BOTTOM_NAVIGATION_SELECT_ITEM";

    private Tab2Fragment tab2Fragment;
    private Tab3Fragment tab3Fragment;
    private Tab4Fragment tab4Fragment;

    private static final int FRAGMENT_TAB1 = 0;
    private static final int FRAGMENT_TAB2 = 1;
    private static final int FRAGMENT_TAB3 = 2;
    private static final int FRAGMENT_TAB4 = 3;

    private BottomNavigationView bottom_navigation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        if (savedInstanceState != null) {
//            tab1Fragment = (Tab1Fragment) getSupportFragmentManager().findFragmentByTag(Tab1Fragment.class.getName());
            tab2Fragment = (Tab2Fragment) getSupportFragmentManager().findFragmentByTag(Tab2Fragment.class.getName());
            tab3Fragment = (Tab3Fragment) getSupportFragmentManager().findFragmentByTag(Tab3Fragment.class.getName());
            tab4Fragment = (Tab4Fragment) getSupportFragmentManager().findFragmentByTag(Tab4Fragment.class.getName());
            // 恢复 recreate 前的位置
            int selectItem = savedInstanceState.getInt(SELECT_ITEM);
            switch (selectItem) {
                case FRAGMENT_TAB2:
                    showFragment(R.id.container, tab2Fragment, Tab2Fragment.class.getName());
                    break;
                case FRAGMENT_TAB3:
                    showFragment(R.id.container, tab3Fragment, Tab3Fragment.class.getName());
                    break;
                case FRAGMENT_TAB4:
                    showFragment(R.id.container, tab4Fragment, Tab4Fragment.class.getName());
                    break;
                default:
            }
            bottom_navigation.setSelectedItemId(selectItem);
        } else {
            showFragment(R.id.container, tab2Fragment, Tab2Fragment.class.getName());
        }
    }

    private void initView() {
//        tab1Fragment = Tab1Fragment.getInstance();
        tab2Fragment = Tab2Fragment.getInstance();
        tab3Fragment = Tab3Fragment.getInstance();
        tab4Fragment = Tab4Fragment.getInstance();

        bottom_navigation = findViewById(R.id.bottom_navigation);
        BottomNavigationViewHelper.disableShiftMode(bottom_navigation);

        bottom_navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_1:
                        showFragment(R.id.container, tab2Fragment, Tab2Fragment.class.getName());
                        break;
                    case R.id.action_2:
                        showFragment(R.id.container, tab3Fragment, Tab3Fragment.class.getName());
                        break;
                    case R.id.action_3:
                        showFragment(R.id.container, tab4Fragment, Tab4Fragment.class.getName());
                        break;
                    default:
                }
                return true;
            }
        });
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // recreate 时记录当前位置 (在 Manifest 已禁止 Activity 旋转,所以旋转屏幕并不会执行以下代码)
        outState.putInt(SELECT_ITEM, bottom_navigation.getSelectedItemId());
    }

    @Override
    public void requestLoading() {

    }

    @Override
    public void resultSuccess(String result) {
        Toast.makeText(this, "结果：" + result.toString(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void resultFailure(String result) {
        Toast.makeText(this, "结果：" + result, Toast.LENGTH_SHORT).show();
    }

    /**
     * fragment中的回调方法
     *
     * @param uri
     */
    @Override
    public void onFragmentInteraction(Uri uri) {
        Toast.makeText(this, "" + uri.getFragment(), Toast.LENGTH_SHORT).show();
    }
}
